package com.estasenposse.tdam.parsers;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import java.net.URL;
import javax.xml.parsers.SAXParser;
import java.net.MalformedURLException;
import javax.xml.parsers.SAXParserFactory;

import com.estasenposse.tdam.entities.Noticia;
import com.estasenposse.tdam.entities.Publicity;

public class RssParserSax {
	private URL rssUrl;

	public RssParserSax(String url) {
		try {
			this.rssUrl = new URL(url);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	public List<Noticia> parse() {
		SAXParserFactory factory = SAXParserFactory.newInstance();

		try {
			SAXParser parser = factory.newSAXParser();
			RssHandler handler = new RssHandler();
			parser.parse(this.getInputStream(), handler);
			return handler.getNoticias();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	//parser de RSS Publicity
	public List<Publicity> parsePublicity(){
		SAXParserFactory factory = SAXParserFactory.newInstance();

		try {
			SAXParser parser2 = factory.newSAXParser();
			RssPublicityHandler handler = new RssPublicityHandler();
			parser2.parse(this.getInputStream(), handler);
			return handler.getPublicidades();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		
	}

	private InputStream getInputStream() {
		try {
			return rssUrl.openConnection().getInputStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}